//
//  MusicViewController.m
//  Auditory
//
//  Created by 寒竹子 on 15/4/28.
//  Copyright (c) 2015年 寒竹子. All rights reserved.
//
/*
    周榜：ttWeek
    月榜：ttMonth
    季榜：ttQuarter
    年榜：ttYear
    全部：ttAll
 */
#define Type @"ttQuarter"

#import "AllMusicViewController.h"
#import "songListModel.h"
#import "MusicListCell.h"
#import "PlayMusicViewController.h"
#import "RootNavigationController.h"
#import "UIViewController+MMSpinner.h"
#import "Globle.h"

@interface AllMusicViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic, strong) NSMutableArray * dataArray;

@property (nonatomic, strong) songListModel * selectedModel; // 已经选中的播放源

@end

@implementation AllMusicViewController

- (instancetype)init
{
    if (self = [super init]) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    
    return self;
}

/**
 * @brief  下载数据
 *
 * @param
 * @return
 */
- (void)downloadData
{
    NSMutableDictionary * params = [[NSMutableDictionary alloc] init];
    NSString * uuid = @"85b8336a-607f-33ed-b27d-7622f2dcffa3";
    NSDate * nowDate = [NSDate date];
    NSTimeInterval timeInterval = [nowDate timeIntervalSince1970];
    
    NSNumber * timeStamp = [NSNumber numberWithLongLong:timeInterval];
    NSNumber * pageSize = [NSNumber numberWithInt:20];
    
    [params setValue:uuid forKey:@"uuid"];
    [params setValue:timeStamp forKey:@"timestamp"];
    [params setValue:pageSize forKey:@"pagesize"];
    [params setValue:@"scHotSongs" forKey:@"category"];
    [params setValue:Type forKey:@"type"];
    [params setValue:@0 forKey:@"page"];
    
    [self addMMSpinner]; // 加载动画
    [Network getJSONWithBaseURL:SongerList_BaseURL params:params returnCall:^(id JSON, NSError *error) {
        NSDictionary * dict = (NSDictionary *)JSON;
        
        NSArray * arr = dict[@"songs"];
        for (int i = 0; i < arr.count; i++) {
            NSDictionary * dict = [arr objectAtIndex:i];
            songListModel * model = [[songListModel alloc] init];
            [model setValuesForKeysWithDictionary:dict];
            model.isPlaying = NO;
            [_dataArray addObject:model];
        }
        
        [self removeMMSpinner]; // 移除动画
        
        [self.view addSubview:self.tableView];
        [_tableView reloadData];
    }];
}

/**
 * @brief  tableView
 *
 * @param
 * @return
 */
- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
     
        _tableView.tableFooterView = [[UIView alloc] init];
        
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.showsVerticalScrollIndicator = NO;
        
        if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) {
            [_tableView setLayoutMargins:UIEdgeInsetsMake(0, 70.0f, 0, 0)];
        }
        
        if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {
            [_tableView setSeparatorInset:UIEdgeInsetsMake(0, 70.0f, 0, 0)];
        }
    }
    
    return _tableView;
}

/**
 * @brief  初始化UI
 *
 * @param
 * @return
 */
- (void)setupUI
{
    self.view.backgroundColor = RGB(248, 248, 248, 1.0f);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self downloadData];
    [self setupUI];
}


#pragma mark - UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsMake(0, 70.0f, 0, 0)];
    }
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 70.0f, 0, 0)];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellId = @"cellID";
    
    MusicListCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (!cell) {
        cell = [[MusicListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    
    cell.indexPath = indexPath;
    songListModel * model = _dataArray[indexPath.row];
    cell.model = model;
    
    UIView * backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 70.0f)];
    backView.backgroundColor = RGB(200, 200, 200, 1.0f);
    cell.selectedBackgroundView = backView;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    songListModel * model = _dataArray[indexPath.row];
    PlayMusicViewController * playVc = [PlayMusicViewController sharedPlayController];
    playVc.model = model;
    playVc.dataArray = self.dataArray;
    playVc.index = indexPath.row;
    /*
     检测是否是正在播放的那个音乐源
     如果是 则直接跳转到播放页面
     如果不是 则切换音乐源 切换到播放页面
     */
    if (_selectedModel != model) {
        _selectedModel = model;
        [playVc playMusicWithSongLink:_selectedModel];
        [Globle sharedGloble].playingIndex = indexPath.row;
    }
    
    // 重新刷新表格
    [tableView reloadData];
    
    [self.rootNav pushViewController:playVc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
